<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
//header("Content-Type: text/html; charset=utf8");
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

include_once '../../php/dbconfig.php';
include_once '../import/import.php';
include_once '../export/export.php';
include_once '../dataaccess/cs_parameter.php';
include_once '../uploadFile/upload.php';
include_once './user.php';




$gUid = new CalendarUser();
$login_id = $gUid->isAdminUser();
$isOnlyAdmin = $gUid->isOnlyAdminUser();
$tableStr = "";
/*
  if($login_id)
  {
  echo "<br>uid: is admin role";
  }
  else
  {echo "<br>uid: is not admin role";}
 */
if (!$login_id) {
    // Header("HTTP/1.1 303 See Other");
    //Header("Location: /amcnn"); //返回根站點
    //exit;
    //$url = "/amcnn/?q=admin"; //看實際情況配置
    $cs_parameter = new cs_parameter();
    $url = $cs_parameter->getHomePage();
    echo '<SCRIPT LANGUAGE="JavaScript">';
    echo "parent.location.href='" . $url . "'";
    echo "</SCRIPT>";
    exit;
}

//導入
if (isset($_POST['selectOpImport'])) {
    //debug 
    //print_r($_FILES);
    $selectOpImport = $_POST['selectOpImport'];
    $export_type = "";
    switch ($selectOpImport) {
        case "1":
            $export_type = "tmp_event";
            break;
        case "2":
            $export_type = "tmp_city";
            break;
        case "3":
            $export_type = "tmp_venue";
            break;
        case "4":
            $export_type = "tmp_event_type";
            break;
        default:
            break;
    }
    if (isset($_POST['hiddenSelectOpImportValue']) && $_POST['hiddenSelectOpImportValue'] == "btUploadFile") {
        echo upload($export_type);
    }
    if (isset($_POST['hiddenSelectOpImportValue']) && $_POST['hiddenSelectOpImportValue'] == "btApplyData") {
        echo apply($export_type);
    }

    exit;
}
//查詢並導出event
if (isset($_POST['hiddenSearchExportEventValue']) && $_POST['hiddenSearchExportEventValue'] == "btSearch") {
    echo search();
    exit;
}

function upload($export_type) {
    $strResult = "";
    $arrayFileTypes = array("image/jpeg", "application/octet-stream", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    $extName = 'xls,xlsx';
    $strFileInputTypeName = "uploadfile";

    $cs_parameter = new cs_parameter();
    $intFileSize = $cs_parameter->getUploadFileSize(); //300000;

    $uploadPath = $cs_parameter->getUploadPath();
    $stringUploadDir = $_SERVER["DOCUMENT_ROOT"] . $uploadPath; // "/upload"; //最后没有/
    //开始上传
    $uploadFile = new PHPUploadFile();
    $upFileName = $uploadFile->uploadFile($strFileInputTypeName, $arrayFileTypes, $extName, $intFileSize);
    //上传完成
    $strResult = $strResult . '<br>上傳文件完成，文件存放於： ' . $upFileName; //OK

    $importExcel = new ImportExcel();
    $exportExcel = new ExportExcel($export_type);
    switch ($export_type) {
        case "tmp_event":
            $result = $importExcel->StartImportExcel($upFileName, $export_type, 0);
            break;
        case "tmp_city":
            $result = $importExcel->StartImportExcel($upFileName, $export_type, 0);
            break;
        case "tmp_venue":
            $result = $importExcel->StartImportExcel($upFileName, $export_type, 0);
            break;
        case "tmp_event_type":
            $result = $importExcel->StartImportExcel($upFileName, $export_type, 0);
            break;
        default:
            break;
    }

    if ($result == 'Finished') {
        $strResult = $strResult . '<br>文件寫入數據庫臨時表成功';
    } else {
        $strResult = $strResult . '<br>文件寫入數據庫臨時表失敗 '.$result;
    }

    //
    $totalErrorCount = $exportExcel->getHasErrorRecordNum();
    $totalCount = $exportExcel->getTotalImportRecordNum();
    $totalPassCount = $exportExcel->getTotalPassRecordNum();

    $errorFilePath = $exportExcel->downloadHasErrorRecors();
    $errorFilePath = "http://" . $errorFilePath;
    $strResult = $strResult . "</br>有錯誤的記錄數為: " . $totalErrorCount;
    $strResult = $strResult . "&nbsp;&nbsp;<a href='" . $errorFilePath . "'>下載此文件</a>";

    $passFilePath = $exportExcel->downloadPassRecors();
    $passFilePath = "http://" . $passFilePath;
    $strResult = $strResult . "</br>完全通過校驗的記錄數為: " . $totalPassCount;
    $strResult = $strResult . "&nbsp;&nbsp;<a href='" . $passFilePath . "'>下載此文件</a>";

    $allFilePath = $exportExcel->downloadAllRecors();
    $allFilePath = "http://" . $allFilePath;
    $strResult = $strResult . "</br>總記錄數為: " . $totalCount;
    $strResult = $strResult . "&nbsp;&nbsp;<a href='" . $allFilePath . "'>下載此文件</a>";
    return $strResult;
}

function apply($export_type) {
    $exportExcel = new ExportExcel($export_type);
    $result = $exportExcel->applyPassRecords();
    $exportExcel->deleteApplyPassRecords();
    return '<br>已經將通過校驗的數據從臨時表轉移到正式表.';
}

function search() {
    $strResult = "";
    $festival = $_POST["festival"];
    $subject = $_POST["subject"];
    $event_type = $_POST["event_type"];
    $city = $_POST["city"];
    $venue = $_POST["venue"];
    $start_time = $_POST["start_time"];
    $end_time = $_POST["end_time"];
    $exportExcel = new ExportExcel();
    $exportFile = $exportExcel->downloadEvent($festival, $subject, $event_type, $city, $venue, $start_time, $end_time);

    $exportFile = "http://" . $exportFile;
    $strResult = $strResult . "查詢結果保存在此文件中: ";
    $strResult = $strResult . "<a href='" . $exportFile . "'>下載此文件</a>";
    return $strResult;
}
?>
<html>
    <head>
        <script src="../../src/jquery.js" type="text/javascript"></script>
        <script src="../../src/Plugins/jquery.form.2.84.js" type="text/javascript"></script>
        <script src="../../src/json2.js" type="text/javascript"></script>
        <script src="../../src/Plugins/jquery.alerts.js" type="text/javascript"></script>
        <link href="../../src/Plugins/jquery.alerts.css" rel="stylesheet" type="text/css" media="screen" />
        <link href="cs_cfg.css" rel="stylesheet" type="text/css" media="screen" />
        <script type="text/javascript">  
            $(document).ready(function()
            {
                divCSMenuClick(1);//初始菜單顯示
                $('#formCSImportExport').ajaxForm(); 
                //---------------
                $("#btConfirm").click(function(){
                    //alert($("#selectOp").val());
                    
                    $.post(
                    "jCfg.php",
                    {selectOp:$("#selectOp").val(),
                        where:$("#txtSearch").val()
                    },
                    function(data){
                        $("#showdata").html(data);
         
                    }
                )
                });
 
                //--------------
                $("#btLoadConfiguration").click(function(){
                    $.post(
                    "jCfg.php",
                    {selectOp:4},
                    function(data){
                        $("#showConfiguration").html(data);
                    }
                )
                });
 
                //--------------
                $('#submitUploadFile').click(function() {
                    $("#hiddenSelectOpImportValue").val("btUploadFile");
                     
                    $("#formCSImportExport").ajaxSubmit(function(data)
                    {
                        $("#divCSImportExportResult").html(data);
                    });
                    //return false;
                });
                //--------------
                $("#btApply").click(function(){
                    $("#hiddenSelectOpImportValue").val("btApplyData");
                    $("#formCSImportExport").ajaxSubmit(function(data)
                    {
                        $("#divCSImportExportResult").html(data);
                    });
                });
                //-------------------------
                $("#btSearch").click(function(){
                    $("#hiddenSearchExportEventValue").val("btSearch");
                    $("#formCSSearchExportEvent").ajaxSubmit(function(data)
                    {
                        $("#divCSSearchExportEventResult").html(data);
                    });
                });
                //-------------------------
            });
            function divCSMenuClick(s)
            {
                switch(s)
                {
                    case 1:
                        $("#divCSImportExport").show();
                        $("#divCSSearchExportEvent").hide();
                        $("#divCSCRUDCityVenueEventType").hide();
                        $("#divCSConfiguretion").hide();
                        break
                    case 2:
                        $("#divCSSearchExportEvent").show();
                        $("#divCSImportExport").hide();
                        $("#divCSCRUDCityVenueEventType").hide();
                        $("#divCSConfiguretion").hide();
                        break
                    case 3:
                        $("#divCSCRUDCityVenueEventType").show();
                        $("#divCSImportExport").hide();
                        $("#divCSSearchExportEvent").hide();
                        $("#divCSConfiguretion").hide();
                        break
                    case 4:
                        $("#divCSConfiguretion").show();
                        $("#divCSImportExport").hide();
                        $("#divCSSearchExportEvent").hide();
                        $("#divCSCRUDCityVenueEventType").hide();
                        break
                    default:
                        alert(s);
                        break
                }
            }
          
        </script>
    </head>
    <body>
        <div id="divCSMenu">
            <a href="" onclick="divCSMenuClick(1);return false;">導入基礎數據</a>&nbsp;&nbsp;<a href="" onclick="divCSMenuClick(2);return false;">導出EVENT數據</a>&nbsp;&nbsp;<a href="" onclick="divCSMenuClick(3);return false;">管理基礎數據</a>&nbsp;&nbsp;<a href="" onclick="divCSMenuClick(4);return false;">管理配置數據</a>
            &nbsp;&nbsp;<a href="
            <?php 
            $cs_parameter = new cs_parameter();
            echo $cs_parameter->getHomePage();
            ?>
                           ">返回主頁</a>
            <br>
            --------------------------------------------------------------------
        </div>
        <div id="divCSImportExport"> 
            <form id="formCSImportExport" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
                <select name="selectOpImport" id="selectOpImport">   
                    <option value="1">Event(事件)</option>   
                    <option value="2">城市</option>   
                    <option value="3">場地</option>   
                    <option value="4">類型</option>   
                </select>
                <input type="hidden" id="hiddenSelectOpImportValue" name="hiddenSelectOpImportValue" value=""></input>
                請選擇一個要上傳的EXCEL文檔:
                <input type="file" name="uploadfile" id="uploadfile"/>
            </form>  
            <input type="button" name="uploadFile" value="上傳並導入文檔" id="submitUploadFile"/>
            <input type="button" value="應用合法數據" id="btApply" name="btApply"></input>
            <br>
            <div id="divCSImportExportResult"></div>
        </div>
        <div id="divCSSearchExportEvent">
            <form id="formCSSearchExportEvent" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
                <table  border="0"cellspacing="1" cellpadding="0">
                    <tr><td>所屬節目:</td><td><input type="text" name="festival" id="festival"/></td><td></td></tr>
                    <tr><td>節目名稱:</td><td><input type="text" name="subject" id="subject"/></td><td>如：睡美人與明日之星</td></tr>
                    <tr><td>種類:</td><td><input type="text" name="event_type" id="event_type"/></td><td>如：音樂</td></tr>
                    <tr><td>地區:</td><td><input type="text" name="city" id="city"/></td><td>如：台北</td></tr>
                    <tr><td>場地:</td><td><input type="text" name="venue" id="venue"/></td><td>如：國家音樂廳</td></tr>
                    <tr><td>開始時間:</td><td><input type="text" name="start_time" id="start_time"/></td><td>如：2010-12-17 00:00:00</td></tr>
                    <tr><td>結束時間:</td><td><input type="text" name="end_time" id="end_time"/></td><td>如：2011-09-11 23:59:59</td></tr>
                    <tr><td></td><td><input type="button" value="查询" id="btSearch" name="btSearch"></input></td><td></td></tr>
                </table>
                <input type="hidden" id="hiddenSearchExportEventValue" name="hiddenSearchExportEventValue" value=""></input>
            </form> 
            <div id="divCSSearchExportEventResult"></div>
        </div>
        <div id="divCSCRUDCityVenueEventType">
            <div>
                請選擇基礎數據類型:<select name="selectOp" id="selectOp">   
                    <option value="1">城市</option>   
                    <option value="2">場地</option>   
                    <option value="3">類型</option>   
                </select><input type="text" value="" id="txtSearch"/><input type="button" value="查詢" id="btConfirm"></input>
            </div>
            <br>
            <div id="showdata" name="showdata"></div>
            <input type="hidden" id="hiddenMsg"></input>
            <input type="hidden" id="hidden_old_context"></input>
        </div> 
        <?php
        if ($isOnlyAdmin) {
            echo '<div id="divCSConfiguretion">';
            echo '<input type="button" value="讀取配置表" id="btLoadConfiguration"></input>';
            echo '<br>';
            echo '<div id="showConfiguration" name="showConfiguration"></div>';
            echo '</div>';
        }
        ?>


    </body>
</html>